const fs = require("fs");
const path = require("path");
const showdown = require("showdown");
const jsdom = require("jsdom");
const { title } = require("process");

const converter = new showdown.Converter()
const dom = new jsdom.JSDOM()
class Article {
    constructor(data, path) {
        this.data = data
        this.path = path
        const el = dom.window.document.createElement("p")
        el.innerHTML = converter.makeHtml(data.split("\n")[0])
        this.title = el.textContent
    }
}
class Dir {
    constructor(title, nowPath, articles, headerLevelStart) {
        this.title = title;
        this.path = nowPath;
        this.articles = []
        this.headerLevelStart = headerLevelStart
        for (const i of articles) {
            if (fs.existsSync(path.join(nowPath, i))) {
                if (fs.statSync(path.join(nowPath, i)).isDirectory()) {
                    if (fs.existsSync(path.join(nowPath, i, "index.json"))) {
                        const index = JSON.parse(fs.readFileSync(path.join(nowPath, i, "index.json")))
                        this.articles.push(new Dir(index.title, path.join(nowPath, i), index.articles, index.headerLevelStart || this.headerLevelStart + 1))
                    } else {
                        throw new Error(`在目录${nowPath}中没有index.json`)
                    }
                } else {
                    let content = fs.readFileSync(path.join(nowPath, i), "utf-8")
                    converter.setOption("headerLevelStart", headerLevelStart)
                    content = converter.makeMarkdown(converter.makeHtml(content), dom.window.document)
                    this.articles.push(new Article(content, path.join(nowPath, i)))
                }
            } else {
                if (fs.existsSync(path.join(nowPath, i + ".md"))) {
                    let content = fs.readFileSync(path.join(nowPath, i + ".md"), "utf-8")
                    converter.setOption("headerLevelStart", headerLevelStart)
                    content = converter.makeMarkdown(converter.makeHtml(content), dom.window.document)
                    this.articles.push(new Article(content, path.join(nowPath, i + ".md")))
                }
            }
        }
    }
    get data() {
        let data = ""
        for (const i of this.articles) {
            data += i.data
        }
        return data
    }
    copyImage() {
        if (fs.existsSync(path.join(this.path, "image", "vscode"))) {
            fs.readdirSync(path.join(this.path, "image", "vscode")).forEach(i => fs.copyFileSync(path.join(this.path, "image", "vscode", i), path.join(process.cwd(), "dist", "image", "vscode", i)))
        } for (const i of this.articles) {
            i.copyImage && i.copyImage()
        }
    }
}
const articlePath = path.join(process.cwd(), "article")
const rootConfig = JSON.parse(fs.readFileSync(path.join(articlePath, "index.json")))
fs.mkdirSync(path.join(process.cwd(), "dist", "image", "vscode"), { recursive: true })
const rootTree = new Dir(rootConfig.title, articlePath, rootConfig.articles)
console.log(rootTree)
fs.writeFileSync(path.join(process.cwd(), "dist", "dist.md"), rootTree.data)
rootTree.copyImage()
// while (true) {}